<?php
namespace app\admin\controller;
use think\view;
use think\Db;
use think\Session;
use Qiniu\Auth as Auth;
class Service extends CommonController
{
    //
    public function index()
    {
        if(!empty($res['p'])){
            $p = $res['p'];
            $this->assign('p',$p);

        }

        $count = DB::name('service')
            ->where(array(
                'state'=>1
            ))
            ->count(); //计算记录数
        $limitRows = 10; // 设置每页记录数
        $p = new AjaxPage($count, $limitRows,"news"); //第三个参数是你需要调用换页的ajax函数名

        $limit_value = $p->firstRow . "," . $p->listRows;
        $list = DB::name('service')
            ->where('state','between','1,2')
            ->order('uptime desc')
            ->limit($limit_value)
            ->select();
        $page = $p->show(); // 产生分页信息，AJAX的连接在此处生成


        $this->assign('list',$list);

        $this->assign('page',$page);
        return $this->fetch();
    }

    //新增客服
    public  function  service_add(){
        if($_POST){
            $_POST['uptime'] = date('Y-m-d H:i:s',time());

            $_POST['password'] = md5( $_POST['password']);
            $_POST['state'] = 1;//新增
            $res = Db::name('service')->where(
                array(
                    'num'=>$_POST['num'],
                    'state'=>'1'
                )
            )->find();
            if($res){
                $this->error('账号已存在，请重新填写其他账号');
            }else{
                $add = Db::name('service')->insert($_POST);
                if($add){
                    $this->success('添加成功', url('Service/index'));
                }else{
                    $this->error('添加失败');
                }
            }

        }
        return $this->fetch();
    }
    //编辑客服信息
    public function service_edit()
    {
        if($_GET){
            $map['id']  = $_GET['id'];
            $list = Db::name('service')->where($map)->find();
            $this->assign('list',$list);
        }
        if($_POST){
            $map['id']  = $_POST['id'];
            $_POST['uptime'] = date('Y-m-d H:i:s',time());
            $list = Db::name('service')->update($_POST);
            if($list){
                $this->success('修改成功', url('Service/index'));

            }else{
                $this->error('修改失败');
            }
        }

        return $this->fetch();
    }


    //删除客服信息（只改变状态 不删除）
    public  function service_del(){
        if($_POST){
            $map['id'] = $_POST['id'];
            $map['state'] = '-1';  //删除的状态
            $res = Db::name('service')->update($map);
            if($res){
                return show(1,'删除成功');
            }else{
                return show(0,'删除失败');
            }
        }

    }

    /*
    * 禁用
    */
    public function yes(){
        $map['id'] = $_POST['id'];
        $_POST['state'] = 2;
        $res = Db::name('service')->where($map)->update($_POST);;
        if($res){
            return show(1,'禁用成功！');
        }else{
            return show(0,'禁用失败！');
        }
    }
    /*
    * 解禁
    */
    public function no(){
        $map['id'] = $_POST['id'];
        $_POST['state'] = 1;
        $res = Db::name('service')->where($map)->update($_POST);;
        if($res){
            return show(1,'解禁成功！');
        }else{
            return show(0,'解禁失败！');
        }
    }



}
